package me.dz.dreamcleaner.command;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.*;
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.InvocationTargetException;
import java.util.Objects;

import static me.dz.dreamcleaner.utils.MsgUtils.*;
import static me.dz.dreamcleaner.command.ICommand.*;

public class Cmd implements CommandExecutor {
    @Override
    public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
        if (args.length > 0){
            ECommand eCommand = getECommand(args[0]);
            if (eCommand != null){
                if (sender.isOp() || !eCommand.op()){
                    if (sender instanceof Player || !eCommand.player()){
                        try {
                            Objects.requireNonNull(getMethod(args[0])).invoke(getICommand(args[0]),sender,label,args);
                        } catch (IllegalAccessException | InvocationTargetException e) {
                            e.printStackTrace();
                        }
                    }else {
                        chat(sender,"&4只有玩家才可以使用这个命令！");
                    }
                    return true;
                }
            }
        }
        help(sender,label);
        return false;
    }
}
